//封装分类业务

import { getCategoryAPI } from "@/apis/category";
import { ref,watch,onMounted } from "vue";
import { useRoute } from "vue-router";

export function useCategory(){
    const categoryData = ref({});
    const route = useRoute();

    //获取一级路由参数
    const getCategoryData = async () => {
    const res = await getCategoryAPI(route.params.id); // route.params.id :获取路由参数
        categoryData.value = res.result
    };

    // 监听路由参数变化
    watch(() => route.params.id, async () => {
        await getCategoryData(); // 当路由参数变化时重新获取分类数据
    });

    onMounted(async () => {
        await getCategoryData();
    });
    return {
        categoryData
    }
}

